﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Practices.Prism.ViewModel;
using PetroMentor.Service.Common;
using HSE.Common;
using PetroMentor.Service;
using DIST.LR.ZHJG.Events;

namespace HSE
{
    public class LoginViewModel : NotificationObject
    {
        private string _selectSysname;
        public string SelectSysname
        {
            get
            {
                return this._selectSysname;
            }

            set
            {

                this._selectSysname = value;
                this.RaisePropertyChanged("SelectSysname");
            }
        }

        private string _sys2name = "HSE动态监控";
        public string Sys2name
        {
            get
            {
                return this._sys2name;
            }

            set
            {

                this._sys2name = value;
                this.RaisePropertyChanged("Sys2name");
            }
        }

        private string _sys1name = "HSE管理维护";
        public string Sys1name
        {
            get
            {
                return this._sys1name;
            }

            set
            {

                this._sys1name = value;
                this.RaisePropertyChanged("Sys1name");
            }
        }

        private string _sys1 = "Images/sys01.png";
        public string Sys1
        {
            get
            {
                return this._sys1;
            }

            set
            {
            	
                this._sys1 = value;
                this.RaisePropertyChanged("Sys1");
            }
        }

        private string _sys2 = "Images/sys02.png";
        public string Sys2
        {
            get
            {
                return this._sys2;
            }

            set
            {

                this._sys2 = value;
                this.RaisePropertyChanged("Sys2");
            }
        }

        private string _selectSys = "";
        public string SelectSys
        {
            get
            {
                return this._selectSys;
            }

            set
            {

                this._selectSys = value;
                this.RaisePropertyChanged("SelectSys");
            }
        }

        private string _username;
        public string Username
        {
            get { return this._username; }

            set
            {
                this._username = value;
                this.RaisePropertyChanged("Username");
            }
        }

        private string _password;
        public string Password
        {
            get { return this._password; }

            set
            {
                this._password = value;
                this.RaisePropertyChanged("Password");
            }
        }

        public string DeptID { get; set; }

        public string DeptName { get; set; }


        const string handler = "{0}";

        public void Login()
        {
            Uri endpoint = new Uri(String.Format("{0}LoginHandler.ashx?{1}={2}&4{3}={1}&{5}={6}&{7}={8}",SystemVar.ServiceUrl,Consts.LOGIN_DEPTID,this.DeptID,Consts.LOGIN_DEPTNAME ,this.DeptName,Consts.LOGIN_USERNAME, this.Username,Consts.LOGIN_PASSWORD, this.Password));
            WebClient client = new WebClient();

            //在异步资源下载操作完成时发生
            client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted);
            client.DownloadStringAsync(endpoint);
        }

        private void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            try
            {
                if (e.Result != null)
                {
                    T_CFG_USER user = Util.JsonToObject<T_CFG_USER>(e.Result) as T_CFG_USER;
                    if (user != null)
                    {
                        EventAggregatorRepository.EventAggregator.GetEvent<HSE.Events.LoginClickEvent>().Publish(user);
                    }

                }
            }

            catch (Exception ex)
            {
                MessageBox.Show(string.Format("获取用户信息发生异常！{0}", ex.Message));
            }
        }
        
    }
}
